package study.spring.mvc.customContainerInitializer;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;

@HandlesTypes(HandledInterface.class)
public class MyServletContainerInitializer implements ServletContainerInitializer {

  @Override
  public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
    System.out.println("servlet初始化调用");
    List<HandledInterface> initializers = new LinkedList<>();
    for (Class<?> waiClass : classes) {
      // Be defensive: Some servlet containers provide us with invalid classes,
      // no matter what @HandlesTypes says...
        try {
          initializers.add((HandledInterface) waiClass.getConstructor().newInstance());
        }
        catch (Throwable ex) {
          throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);
      }
    }
    for (HandledInterface initializer : initializers) {
      initializer.onStartup(servletContext);
    }
  }
}
